<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="菜单名称" prop="menuName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入菜单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList"/>
    </el-row>

    <el-table
      v-loading="loading"
      :data="menuList"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="菜单名称" width="170" align="left"/>
      <el-table-column prop="icon" label="图标" align="center">
        <template slot-scope="scope">
          <img :src="getIcon(scope.row.icon)" style="width: 20px">
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" align="center"/>
      <el-table-column prop="url" label="菜单地址" width="120" align="center"/>
      <el-table-column prop="zjurl" label="组件路径" width="120" align="center"/>
      <el-table-column prop="type" label="菜单类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type===1">目录</span>
          <span v-if="scope.row.type===2">页面</span>
        </template>
      </el-table-column>
      <el-table-column prop="redirecturl" label="重定向" align="center"/>
      <!--      <el-table-column prop="menuStatus" label="状态" :formatter="menuStatusFormat" align="center"/>-->
      <!--      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级菜单" prop="parentId">
              <treeselect
                v-model="form.parentId"
                :options="menuOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级菜单"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="菜单类型" prop="type">
              <el-radio-group v-model="form.type">
                <el-radio :label="1">目录</el-radio>
                <el-radio :label="2">页面</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="菜单图标" prop="icon">
              <el-popover
                placement="bottom-start"
                width="460"
                trigger="click"
                @show="$refs['iconSelect'].reset()"
              >
                <IconSelect ref="iconSelect" @selected="selected"/>
                <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" readonly>
                  <img v-if="form.icon" slot="prefix" :src="getIcon(form.icon)"
                       style="width: 20px;margin-right: 5px"
                  >
                  <i v-else slot="prefix" class="el-icon-search el-input__icon"/>
                </el-input>
              </el-popover>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入菜单名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单排序" prop="sort">
              <el-input-number v-model="form.sort" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="路由地址" prop="url">
              <el-input v-model="form.url" placeholder="请输入路由地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组件路径" prop="zjurl">
              <el-input v-model="form.zjurl" placeholder="请输入组件路径"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="重定向" prop="redirecturl">
              <el-input v-model="form.redirecturl" placeholder="请输入重定向地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="权限标识" prop="identifying">
              <el-input v-model="form.identifying" placeholder="请输入权限标识"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="菜单状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">启用</el-radio>
                <el-radio :label="2">冻结</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import IconSelect from '@/components/IconSelect'
import icons from '@/components/IconSelect/requireIcons'
import menuData from '@/utils/csData/csData'
import arrMenu from '@/utils/csData/csData'

export default {
  name: 'Menu',
  components: { Treeselect, IconSelect },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 菜单状态数据字典
      // 查询参数
      queryParams: {
        name: undefined,
        roleId: this.$store.state.user.roleId
      },
      selectIcon: 'selectIcon',
      // 表单参数
      form: {
        id: undefined,
        parentId: undefined, // 上级菜单
        type: 2, // 菜单类型
        icon: undefined, // 菜单图标
        name: undefined, // 菜单名称
        sort: 0, // 菜单排序
        status: 1, // 显示状态
        url: undefined, // 路由地址
        zjurl: undefined, // 组件路径
        redirecturl: undefined, // 重定向
        identifying: undefined // 权限标识
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '菜单顺序不能为空', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '路由地址不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getIcon(img) {
      if (icons.indexOf(img) > -1) {
        return require('@/assets/images/topMenu/' + img + '.png')
      } else {
        return ''
      }
    },
    // 选择图标
    selected(name) {
      this.form.icon = name
    },
    /** 查询菜单列表 */
    async getList() {
      this.loading = true
      // const menuData = menuData
      const res = await this.$http.get('/sysResources/list', this.queryParams)
      if (res.code == '200') {
        this.menuList = this.handleTree(res.data)
        this.loading = false
      }
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    /** 查询菜单下拉树结构 */
    async getTreeselect() {
      const params = { roleId: this.$store.state.user.roleId }
      const res = await this.$http.get('/sysResources/list', params)
      if (res.code == '200') {
        this.menuOptions = []
        const menu = { id: 0, name: '主类目', children: [] }
        menu.children = this.handleTree(res.data)
        this.menuOptions.push(menu)
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        // identifying: undefined,
        parentId: undefined, // 上级菜单
        type: 2, // 菜单类型
        icon: undefined, // 菜单图标
        name: undefined, // 菜单名称
        sort: 0, // 菜单排序
        status: 1, // 显示状态
        url: undefined, // 路由地址
        zjurl: undefined, // 组件路径
        redirecturl: undefined, // 重定向
        identifying: undefined // 权限标识
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.getTreeselect()
      if (row != null && row.id) {
        this.form.parentId = row.id
      } else {
        this.form.parentId = 0
      }
      this.open = true
      this.title = '添加菜单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      this.form = row
      this.open = true
      this.title = '修改菜单'
      // })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            this.$http.post('/sysResources/modify', this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            this.$http.post('/sysResources/add', this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return this.$http.post('/sysResources/deletes', { entityIds: row.id })
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.select {
  ::v-deep .el-input__inner {
    background-color: #237CFA;
    color: #FFFFFF;
  }

}

//::v-deep .el-input__prefix {
//  position: relative;
//  top: 5px;
//}
</style>

